﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using DevMaker.SIC.Model;
using log4net;


namespace DevMaker.SIC.Dao {
    public class ObservacionDao {

        private static readonly ILog Log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        public Observacion Crear(Turno turno, Observacion observacion) {
            const string storeName = "sp_observaciones_c";
            var conn = new SqlConnection(ConfigurationManager.ConnectionStrings["SIC_Connection"].ToString());
            SqlTransaction trans = null;
            try {
                conn.Open();
                var command = new SqlCommand(storeName, conn) { Transaction = conn.BeginTransaction(), CommandType = CommandType.StoredProcedure };
                trans = command.Transaction;
                command.Parameters.AddWithValue("@id_sucursal", turno.Sucursal.Id);
                command.Parameters.AddWithValue("@cod_sucursal", turno.Sucursal.Codigo);
                command.Parameters.AddWithValue("@id_turno", turno.Id);
                command.Parameters.AddWithValue("@id_supervisor", observacion.Usuario.Id);
                command.Parameters.AddWithValue("@observacion", observacion.Comentario);

                var outParam = command.Parameters.Add("@id", SqlDbType.BigInt);
                outParam.Direction = ParameterDirection.Output;
                command.ExecuteScalar();
                observacion.Id = Convert.ToInt32(outParam.Value);
                trans.Commit();
            } catch (Exception ex) {
                Log.Error(ex);
                observacion.Id = 0;
                if (trans != null)
                    trans.Rollback();
                throw;
            } finally {
                if (conn.State != ConnectionState.Closed)
                    conn.Close();
            }
            return observacion;
        }

        public List<Observacion> ObtienePorTurno (Turno turno){
            const string storeName = "sp_observaciones_por_id_turno";
            var list = new List<Observacion>();
            var conn = new SqlConnection(ConfigurationManager.ConnectionStrings["SIC_Connection"].ToString());
            try {
                conn.Open();
                var command = new SqlCommand(storeName, conn) { CommandType = CommandType.StoredProcedure };

                command.Parameters.AddWithValue("@id_turno", turno.Id);
                var reader = command.ExecuteReader();
                while (reader.Read()) {
                    var p = new Observacion();
                    p.ReadFromDataReader(reader);
                    list.Add(p);
                }
            } catch (Exception ex) {
                Log.Error(ex);
                throw;
            } finally {
                if (conn.State != ConnectionState.Closed)
                    conn.Close();
            }
            return list;
        }
    }
}
